﻿Imports BUS
Imports DTO
Public Class frmNhanBangDiemMon
    Private bangdiem As New List(Of BangDiemDto)

    Private Sub LoadHocKy()
        Dim hkBus As New HocKyBus()
        Dim ds As List(Of HocKyDto)
        ds = hkBus.LayDSHocKy()
        cbbHocKy.DataSource = ds
        cbbHocKy.ValueMember = "MaHK"
        cbbHocKy.DisplayMember = "TenHocKy"
    End Sub
    Private Sub LoadMonHoc()
        Dim mhBus As New MonHocBus()
        Dim ds As List(Of MonHocDto)
        ds = mhBus.LayDSMonHoc()
        cbbMon.DataSource = ds
        cbbMon.DisplayMember = "TenMon" 'ten chuoi giong trong property
        cbbMon.ValueMember = "MaMon"
    End Sub
    Private Sub LoadLopHoc()
        Dim lhBus As New LopHocBus()
        Dim ds As List(Of LopHocDto)
        ds = lhBus.LayDSLop()
        cbbLop.DataSource = ds
        cbbLop.ValueMember = "MaLop"
        cbbLop.DisplayMember = "TenLop"
        lc1 = False
    End Sub
    Private Sub LoadDSHocSinh()
        Dim hsBus As New HocSinhBus()
        Dim ds As DataTable
        ds = hsBus.LayDSHS_Ma(cbbLop.SelectedValue)
        cbbTenHS.DataSource = ds
        cbbTenHS.ValueMember = "MaHS"
        cbbTenHS.DisplayMember = "TenHS"

    End Sub
    Private lc As Boolean = False
    Dim lc1 As Boolean = False
    Private Sub frmNhanBangDiemMon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            lc1 = True
            LoadHocKy()
            LoadLopHoc()
            LoadMonHoc()
            LoadDSHocSinh()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub cbbLop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbLop.SelectedIndexChanged
        If lc1 Then
            Return
        End If
        LoadDSHocSinh()
    End Sub
    Private Function KiemTra() As Boolean
        If Not Char.IsDigit(txtDiem15phut.Text) Or Char.IsControl(txtDiem15phut.Text) Then
            MessageBox.Show("Diem phai la so!")
            Return False
        ElseIf Not Char.IsDigit(txtDiem1tiet.Text) Or Char.IsControl(txtDiem1tiet.Text) Then
            MessageBox.Show("Diem phai la so!")
            Return False
        ElseIf Not Char.IsDigit(txtDeimcuoiHK.Text) Or Char.IsControl(txtDeimcuoiHK.Text) Then
            MessageBox.Show("Diem phai la so!")
            Return False
        End If
        If Double.Parse(txtDiem15phut.Text) > 10 Or Double.Parse(txtDiem15phut.Text) < 0 Then
            MessageBox.Show("Diem khong hop le")
            Return False
        End If
        Return True
    End Function
    Dim i = 0
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If KiemTra() = True Then
            i = i + 1
            Dim ten As String = cbbTenHS.Text
            Dim mahs As Int32 = cbbTenHS.SelectedValue
            Dim diem15phut As Double = txtDiem15phut.Text
            Dim diem1tiet As Double = txtDiem1tiet.Text
            Dim diemck As Double = txtDeimcuoiHK.Text
            Dim mamon As Int32 = cbbMon.SelectedValue
            Dim hk As Int32 = cbbHocKy.SelectedValue
            Dim malop As Int32 = cbbLop.SelectedValue
            Dim bd As New BangDiemDto
            'gridViewNhanBangDiem.Rows.Clear()
            gridViewNhanBangDiem.Rows.Add(i, ten, diem15phut, diem1tiet, diemck)
            bd.MaHS = mahs
            bd.MaLop = malop
            bd.MaMonHoc = mamon
            bd.HocKy = hk
            bd.Diem15Phut = diem15phut
            bd.Diem1Tiet = diem1tiet
            bd.DiemCuoiKy = diemck
            bangdiem.Add(bd)
            txtDiem15phut.Text = ""
            txtDiem1tiet.Text = ""
            txtDeimcuoiHK.Text = ""
        End If
    End Sub

    

    Private Sub btnNhan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhan.Click
        Dim i As Integer
        For i = 0 To bangdiem.Count - 1
            BangDiemBus.InsertBangDiem(bangdiem(i).MaHS, bangdiem(i).MaMonHoc, bangdiem(i).HocKy, bangdiem(i).MaLop, bangdiem(i).Diem15Phut, bangdiem(i).Diem1Tiet, bangdiem(i).DiemCuoiKy)
        Next
        gridViewNhanBangDiem.Rows.Clear()
        bangdiem.Clear()
        MessageBox.Show("Bạn Thêm thành công")
    End Sub

    Private Sub gridViewNhanBangDiem_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridViewNhanBangDiem.SelectionChanged
        If lc = False Then
            lc = True
            Return
        End If
        If gridViewNhanBangDiem.SelectedRows.Count = 0 Then
            Return
        End If
        Dim index As Int32 = gridViewNhanBangDiem.CurrentRow.Index
        If index < gridViewNhanBangDiem.Rows.Count - 1 Then
            'cbbTenHS.Text = gridViewNhanBangDiem.Rows(index).Cells(1).Value
            cbbLop.Text = bangdiem(index).MaLop
            cbbMon.Text = bangdiem(index).MaMonHoc
            cbbHocKy.Text = gridViewNhanBangDiem.Rows(index).Cells(1).Value
            cbbHocKy.Text = bangdiem(index).HocKy
            txtDiem15phut.Text = bangdiem(index).Diem15Phut
            txtDiem1tiet.Text = bangdiem(index).Diem1Tiet
            txtDeimcuoiHK.Text = bangdiem(index).DiemCuoiKy
            lc = True
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If gridViewNhanBangDiem.SelectedRows.Count = 0 Then
            MessageBox.Show("Chưa Chọn Dòng Xóa")
            Return
        End If
        Dim index As Integer = gridViewNhanBangDiem.CurrentRow.Index
        If index >= gridViewNhanBangDiem.Rows.Count - 1 Then
            Return
        End If
        gridViewNhanBangDiem.Rows.RemoveAt(index)
        bangdiem.RemoveAt(index)


    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If gridViewNhanBangDiem.SelectedRows.Count = 0 Then
            MessageBox.Show("Chưa Chọn Dòng Xóa")
            Return
        End If
        Dim index As Integer = gridViewNhanBangDiem.CurrentRow.Index
        If index >= gridViewNhanBangDiem.Rows.Count - 1 Then
            Return
        End If
        bangdiem(index).MaHS = cbbTenHS.SelectedValue
        bangdiem(index).MaLop = cbbLop.SelectedValue
        bangdiem(index).HocKy = cbbHocKy.SelectedValue
        bangdiem(index).MaMonHoc = cbbMon.SelectedValue
        bangdiem(index).Diem15Phut = txtDiem15phut.Text
        bangdiem(index).Diem1Tiet = txtDiem1tiet.Text
        bangdiem(index).DiemCuoiKy = txtDeimcuoiHK.Text
        gridViewNhanBangDiem.Rows(index).Cells(1).Value = cbbTenHS.Text
        gridViewNhanBangDiem.Rows(index).Cells(2).Value = txtDiem15phut.Text
        gridViewNhanBangDiem.Rows(index).Cells(3).Value = txtDiem1tiet.Text
        gridViewNhanBangDiem.Rows(index).Cells(4).Value = txtDeimcuoiHK.Text
    End Sub

End Class